package com.yiba.vpn.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/** 
* @author : tang 
* @createtime ：2017年11月7日 上午9:56:46 
* 
*/
public class FileUtils {
	
	private final static Logger LOGGER = LoggerFactory.getLogger(FileUtils.class);
	
	/**
	 * 读取根目录下的文件 返回map
	 * @param fileName
	 * @return
	 */
	public static Map<String, String> loadPropertiesToMap(String folder ,String fileName) {
		String path = FileUtils.class.getClassLoader().getResource("").getPath() + folder +"/" +fileName;
		Properties pro = new Properties();
		try {
			// 指向一个属性文件
			FileInputStream fis = new FileInputStream(path);
			// 加载属性文件
			pro.load(new InputStreamReader(new BufferedInputStream(fis), "utf-8"));
		} catch (Exception e) {
			LOGGER.error(e + "----------读取Properties配置文件失败-------" + path);
		}
		Map<String, String> propertiesMap = new HashMap<String,String>();
		Set<String> proStr = pro.stringPropertyNames();
		for (String key : proStr) {
			System.out.println("key="+key+",value=" + pro.getProperty(key));
			propertiesMap.put(key, pro.getProperty(key));
		}
		return propertiesMap;
	}
	
	
	public static Properties loadProperties(String folder ,String fileName) {
		String path = FileUtils.class.getClassLoader().getResource("").getPath() + folder +"/" + fileName;
		Properties pro = new Properties();
		try {
			// 指向一个属性文件
			FileInputStream fis = new FileInputStream(path);
			// 加载属性文件
			pro.load(new InputStreamReader(new BufferedInputStream(fis), "utf-8"));
		} catch (Exception e) {
			LOGGER.error(e + "----------读取Properties配置文件失败-------" + path);
		}
		return pro;
	}
}
 